class Solution:#最长回文子串
    def longestPalindrome(self, s: str) -> str:
        if not s:
            return ""

        start, max_len = 0, 1

        def expand_around_center(left: int, right: int) -> int:
            # 以left和right为中心，向两边扩展，寻找最大回文子串长度
            while left >= 0 and right < len(s) and s[left] == s[right]:
                left -= 1
                right += 1
            # 返回当前回文串的长度
            return right - left - 1

        for i in range(len(s)):
            # 以i为中心，扩展奇数长度的回文串
            len1 = expand_around_center(i, i)
            # 以i和i+1为中心，扩展偶数长度的回文串
            len2 = expand_around_center(i, i + 1)
            current_max = max(len1, len2)
            if current_max > max_len:
                max_len = current_max
                # 更新起点
                start = i - (max_len - 1) // 2

        return s[start:start + max_len]

#最长回文子串
class Solution:
    if __name__ == "__main__":
        sol = Solution()
        print(sol.longestPalindrome("babad"))
        print(sol.longestPalindrome("cbbd"))
